package com.fyzk.activity.share;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fyzk.adapter.MyInvitationAdapter;
import com.fyzk.base.BaseActivity;
import com.fyzk.bean.FriendsBean;
import com.fyzk.bean.MyInvitationBean;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.dialog.GetAwardDialog;
import com.fyzk.dialog.PublicDialog;
import com.fyzk.net.Commands;
import com.fyzk.utils.LogUtils;
import com.fyzk.view.TopTitleView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

@ContentView(R.layout.activity_my_invitation)
public class MyInvitationActivity extends BaseActivity implements OnRefreshListener {
    @ViewInject(R.id.myInvitationUI_topView)
    TopTitleView topView;
    @ViewInject(R.id.myInvitationUI_recyclerView)
    RecyclerView mRecyclerView;
    @ViewInject(R.id.myInvitationUI_smartRefreshLayout)
    SmartRefreshLayout mSmartRefresh;
    @ViewInject(R.id.myInvitationUI_btnGetAward)
    Button btnGetAward;

    View headerView;
    View emptyView;
    MyInvitationAdapter mAdapter;

    List<FriendsBean> friendsBeans;
    double awardAmount = 0;

    private boolean isShowLoading = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initEvent();
        initDate();
        httpPostFriendsList();
    }

    private void httpPostFriendsList() {
        try {
            JSONObject params = new JSONObject();
            params.put("userno", MySharedPreferences.getInstance().getUserNo());
            httpPostJson(CommandUtil.FRIENDSLIST, Commands.USERINVITEINFO, params, isShowLoading);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initEvent() {
        btnGetAward.setOnClickListener(this);
        topView.setOnTitleClickListener(this);
        mSmartRefresh.setOnRefreshListener(this);
    }

    private void initDate() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.rlv_divider));
        mRecyclerView.addItemDecoration(dividerItemDecoration);
        headerView = LayoutInflater.from(this).inflate(R.layout.header_my_invitation, mRecyclerView, false);
        emptyView = LayoutInflater.from(this).inflate(R.layout.empty_my_invitation, mRecyclerView, false);
        mAdapter = new MyInvitationAdapter();
        mAdapter.addHeaderView(headerView);
        mAdapter.setEmptyView(emptyView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public void onRightClick() {
        finish();
    }

    @Override
    public void onClick(View v) {
        if (awardAmount > 0) {
            showInfoDialog();
        } else {
            showToast("暂无可领取佣金");
        }
    }

    private void showInfoDialog() {
        PublicDialog mDialog = new PublicDialog(this, new PublicDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    showGetAwardDialog();
                }
            }
        });
        mDialog.setTitle("是否领取佣金？");
        mDialog.setNegativeButton("取消");
        mDialog.setPositiveButton("确定");
        mDialog.show();
    }

    private void showGetAwardDialog() {
        GetAwardDialog mDialog = new GetAwardDialog(this, new GetAwardDialog.OnConfirmListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    isShowLoading = true;
                    httpGetAward();
                }
            }
        });
        mDialog.setAward(awardAmount + "米");
        mDialog.setPositiveButton("确定");
        mDialog.show();
    }

    private void httpGetAward() {
        try {
            JSONObject params = new JSONObject();
            params.put("userno", MySharedPreferences.getInstance().getUserNo());
            httpPostJson(CommandUtil.GETAWARD, Commands.USERRECEIVEPROFIT, params, isShowLoading);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isShowLoading = false;
        httpPostFriendsList();
        mSmartRefresh.finishRefresh(1000);
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        LogUtils.e("====result====" + result);
        switch (requestCode) {
            case CommandUtil.FRIENDSLIST:
                if ("0".equals(resultBean.getErrorcode())) {
                    MyInvitationBean myInvitationBean = JSON.parseObject(resultBean.getData(), MyInvitationBean.class);
                    if (myInvitationBean != null) {
                        awardAmount = myInvitationBean.getCanReceiveAmt();
                        friendsBeans = myInvitationBean.getList();
                        mAdapter.setNewData(friendsBeans);
                        if (friendsBeans.size() == 0) {
                            btnGetAward.setVisibility(View.GONE);
                        } else {
                            btnGetAward.setVisibility(View.VISIBLE);
                        }
                    }
                } else {
                    showToast(resultBean.getMsg());
                }
                break;
            case CommandUtil.GETAWARD:
                if ("0".equals(resultBean.getErrorcode())) {
                    isShowLoading = false;
                    httpPostFriendsList();
                }
                showToast(resultBean.getMsg());
                break;
        }
    }

    @Override
    protected void onFailure(int requestCode, String errorcode, String message) {
        LogUtils.e("====errorcode====" + errorcode);
        showToast(message);
    }

}
